﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using RMS.Model;
using RMS.View;
using System.Windows.Forms;

namespace RMS.Controller
{
    public class AcademicDepartmentController
    {
        private AcademicDepartment _model;
        private AcademicDepartmentView _view;

        public AcademicDepartmentController(AcademicDepartment model, AcademicDepartmentView view)
        {
            this._model = model;
            this._view = view;
        }

        public AcademicDepartment Model
        {
            get { return this._model; }
            set { this._model = value; }
        }
        public AcademicDepartmentView View
        {
            get { return this._view; }
            set { this._view = value; }
        }

        public void AddHumanResource(HumanResource hr)
        {
            if (hr == null) return;            
            int retId = 0;
            if ((retId = Persistence.DataManager.Instance.InsertHumanResource(hr, this._model.Id)) > 0)
            {
                hr.Id = retId;
                this._model.HumanResources.Add(hr);
                this._view.RefreshHumanResourcesList(this._model.HumanResources);
            }
        }

        public bool RemoveHumanResource(HumanResource hr)
        {
            if (hr == null) return false;
            foreach (HumanResource currentHr in this._model.HumanResources)
            {
                if (currentHr.Id == hr.Id)
                {
                    Persistence.DataManager.Instance.RemoveHumanResource(currentHr);
                    this._model.HumanResources.Remove(currentHr);
                    this._view.RefreshHumanResourcesList(this._model.HumanResources);
                    return true;
                }
            }
            return false;
        }

        public bool UpdateHumanResource(HumanResource hr)
        {
            if (hr == null) return false;
            foreach (HumanResource currentHr in this._model.HumanResources)
            {
                if (currentHr.Equals(hr))
                {
                    currentHr.Id = hr.Id;
                    currentHr.Username = hr.Username;
                    currentHr.Authority = hr.Authority;
                    currentHr.Title = hr.Title;
                    currentHr.FirstName = hr.FirstName;
                    currentHr.LastName = hr.LastName;
                    currentHr.Address = hr.Address;
                    currentHr.PhoneNumber = hr.PhoneNumber;
                    Persistence.DataManager.Instance.UpdateHumanResource(hr);
                    this._view.RefreshHumanResourcesList(this._model.HumanResources);
                    return true;
                }
            }
            return false;
        }

        public List<HumanResource> SearchHumanResource(string lastName, string firstName = "")
        {
            List<HumanResource> hrList = new List<HumanResource>();
            if (lastName == "" && firstName == "") return null;
            foreach (HumanResource hr in this._model.HumanResources)
            {
                if (lastName != "")
                {
                    if (firstName == "" && lastName == hr.LastName)
                        hrList.Add(hr);
                    if (firstName != "" && lastName == hr.LastName && firstName == hr.FirstName)
                        hrList.Add(hr);
                }
                else
                {
                    if (firstName == hr.FirstName)
                        hrList.Add(hr);
                }
            }
            return hrList;
        }

        public bool ValidateInput()
        {
            if (this._view.TextBoxUsername == "")
            {
#if DEBUG
                throw new ArgumentNullException("username");
#else
                MessageBox.Show("username box empty");
                return false;
#endif
            }
            if (this._view.TextBoxAuthority == "Teacher" && this._view.TextBoxTitle == "")
            {
#if DEBUG
                throw new ArgumentNullException("title");
#else
                MessageBox.Show("title box empty");
                return false;
#endif
            }

            if (this._view.TextBoxFirstName == "")
            {
#if DEBUG
                throw new ArgumentNullException("first name");
#else
                MessageBox.Show("first name box empty");
                return false;
#endif
            }
            if (this._view.TextBoxLastName == "")
            {
#if DEBUG
                throw new ArgumentNullException("last name");
#else
                MessageBox.Show("last name box empty");
                return false;
#endif
            }
            return true;
        }

        public void HandleAddButtonClick()
        {
            if (!this.ValidateInput()) return;            
            UserType authority = UserType.PublicPerson;
            switch (this._view.TextBoxAuthority)
            {
                case "Director":
                    authority = UserType.Director;
                    break;
                case "Teacher":
                    authority = UserType.Teacher;
                    break;
                case "Secretary":
                    authority = UserType.Secretary;
                    break;
            }

            HumanResource humanResource = new HumanResource();
            //humanResource.Id = Convert.ToInt32(this._view.TextBoxId);
            humanResource.Id = 0;
            humanResource.Username = this._view.TextBoxUsername;
            humanResource.Authority = authority;
            humanResource.Title = this._view.TextBoxTitle;
            humanResource.FirstName = this._view.TextBoxFirstName;
            humanResource.LastName = this._view.TextBoxLastName;
            humanResource.Address = this._view.TextBoxAddress;
            humanResource.PhoneNumber = this._view.TextBoxPhoneNumber;

            this.AddHumanResource(humanResource);
        }

        public void HandleRemoveButtonClick()
        {
            HumanResource humanResource = new HumanResource();
            humanResource.Id = Convert.ToInt32(this._view.TextBoxId);
            this.RemoveHumanResource(humanResource);
        }

        public void HandleUpdateButtonClick()
        {
            if (!this.ValidateInput()) return;
            UserType authority = UserType.PublicPerson;
            switch (this._view.TextBoxAuthority)
            {
                case "Director":
                    authority = UserType.Director;
                    break;
                case "Teacher":
                    authority = UserType.Teacher;
                    break;
                case "Secretary":
                    authority = UserType.Secretary;
                    break;
            }

            HumanResource humanResource = new HumanResource();
            humanResource.Id = Convert.ToInt32(this._view.TextBoxId);
            humanResource.Username = this._view.TextBoxUsername;
            humanResource.Authority = authority;
            humanResource.Title = this._view.TextBoxTitle;
            humanResource.FirstName = this._view.TextBoxFirstName;
            humanResource.LastName = this._view.TextBoxLastName;
            humanResource.Address = this._view.TextBoxAddress;
            humanResource.PhoneNumber = this._view.TextBoxPhoneNumber;

            this.UpdateHumanResource(humanResource);
        }

        public void HandleSearchButtonClick()
        {
            this._view.RefreshHumanResourcesList(this.SearchHumanResource(this._view.TextBoxLastName, this._view.TextBoxFirstName));
        }

        public void SyncModelWithDatabase(int id)
        {
            this._model = Persistence.DataManager.Instance.SyncAcademicDepartment(id);
            this._model.HumanResources = Persistence.DataManager.Instance.SyncAcademicDepartmentHumanResources(this._model.Id);
        }

        internal void HandleSearchCancelButtonClick()
        {
            this._view.RefreshHumanResourcesList(this._model.HumanResources);
        }
    }
}
